package cn.ac.iscas.action.thesis;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.ac.iscas.bean.softwareCopyright.SoftwareCopyright;
import cn.ac.iscas.bean.thesis.Thesis;
import cn.ac.iscas.bean.thesis.ThesisDocument;
import cn.ac.iscas.service.thesis.IThesisDocService;
import cn.ac.iscas.service.thesis.IThesisService;

@Controller @Scope("prototype")
public class ThesisManageAction extends ActionSupport{

	/***********KE Start*************/
	/**
	 *处理项目的录入，保存，修改，删除的功能
	 */
	private static final long serialVersionUID = 821015281711723027L;
	@Resource private IThesisService thesisService;
	@Resource private IThesisDocService thesisDocService;
	private Thesis thesis;//接收处理时动态注入
	private ThesisDocument document;//接收处理时动态注入
	
	private File[] word;
	private String[] wordContentType;
	private String[] wordFileName;
	private File[] pdf;
	private String[] pdfContentType;
	private String[] pdfFileName;
	
//	@Resource private IProjectService projectService;//运行时动态注入projectService层
//	@Resource private IProjectDocumentService projectDocumentService;//运行时动态注入projectDocumentService层
//	private Project project;//接收处理时动态注入
//	private ProjectDocument document;//接收处理时动态注入
	/***********KE End*************/
	
	
	/**
	 * 处理项目的详细查询
	 * */
	public String detailQuery()//详细信息查询
	{
		thesis= thesisService.get(Thesis.class, thesis.getId());
		return "detailQuery";//跳转到详细信息界面
	}
	/**
	 * 处理项目的修改
	 * */
	public String edit()//项目信息修改
	{
		thesis = thesisService.get(Thesis.class, thesis.getId());
		return "edit";//跳转修改界面
	}
	/**
	 * 处理项目的更新
	 * */
	public String update() throws IOException
	{
		
		Set<ThesisDocument> documents = thesisService.get(Thesis.class, thesis.getId()).getWord();
    	for(ThesisDocument doc:documents)
    	{
    		doc.setThesis(thesis);
    	}
    	String realpath = ServletActionContext.getServletContext().getRealPath("/documents");
		Set<ThesisDocument> paperWordDocuments = new HashSet<ThesisDocument>();
		Set<ThesisDocument> paperPDFDocuments = new HashSet<ThesisDocument>();
		if(pdf != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<pdf.length;i++)
			{
				ThesisDocument docInfo = new ThesisDocument();
				String realName = UUID.randomUUID().toString()+ getExt(pdfFileName[i]);
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(pdf[i], savefile);
				docInfo.setDocName(pdfFileName[i]);
//				docInfo.setContentType(pdfContentType[i]);
				docInfo.setUploadRealName(realName);
				docInfo.setDocPath(realpath);
				docInfo.setUploadTime(new Date());
				docInfo.setDocumentType("pdf");
				docInfo.setSize(pdf[i].length()/1024);
				docInfo.setThesis(thesis);
				paperWordDocuments.add(docInfo);
			}
	   	System.out.println("上传验收文档中文档成功");
      	System.out.println(realpath);
		}
		if(word != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<word.length;i++)
			{
				ThesisDocument docInfo = new ThesisDocument();
				String realName = UUID.randomUUID().toString()+ getExt(wordFileName[i]);
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(word[i], savefile);
				docInfo.setDocName(wordFileName[i]);
//				docInfo.setContentType(wordContentType[i]);
				docInfo.setUploadRealName(realName);
				docInfo.setDocPath(realpath);
				docInfo.setUploadTime(new Date());
				docInfo.setDocumentType("word");
				docInfo.setSize(word[i].length()/1024);
				docInfo.setThesis(thesis);
				paperWordDocuments.add(docInfo);
			}
	   	System.out.println("上传验收文档中文档成功");
      	System.out.println(realpath);
		}
//		conferencePaper.setPaperType(PaperType.会议论文);
		thesisService.update(thesis);
		for(ThesisDocument doc:documents)
    	{
			thesisDocService.save(doc);
    	}
		for(ThesisDocument doc:paperWordDocuments)
		{
			thesisDocService.save(doc);
		}
		for(ThesisDocument doc:paperPDFDocuments)
		{
			thesisDocService.save(doc);
		}
    	return "success";
	}
	/**
	 * 
	 * 项目保存
	 * */
	public String save() throws IOException
	{
		
		String realpath = ServletActionContext.getServletContext().getRealPath("/documents");
		Set<ThesisDocument> paperWordDocuments = new HashSet<ThesisDocument>();
		Set<ThesisDocument> paperPDFDocuments = new HashSet<ThesisDocument>();
		if(pdf != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<pdf.length;i++)
			{
				ThesisDocument docInfo = new ThesisDocument();
				String realName = UUID.randomUUID().toString()+ getExt(wordFileName[i]);
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(pdf[i], savefile);
				docInfo.setDocName(pdfFileName[i]);
//				docInfo.setContentType(pdfContentType[i]);
				

				System.err.println("pdfContentType:"+pdfContentType[i]);
				
				
				docInfo.setUploadRealName(realName);
				docInfo.setDocPath(realpath);
				docInfo.setUploadTime(new Date());
				docInfo.setDocumentType("pdf");
				docInfo.setSize(pdf[i].length()/1024);
				docInfo.setThesis(thesis);
				paperWordDocuments.add(docInfo);
			}
	   	System.out.println("上传验收文档中文档成功");
      	System.out.println(realpath);
		}
		if(word != null)
		{
			System.out.println("word start1");
			System.out.println(word.length);
			
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<word.length;i++)
			{
				System.out.println("word start1");
				
				ThesisDocument docInfo = new ThesisDocument();
				String realName = UUID.randomUUID().toString()+ getExt(wordFileName[i]);
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(word[i], savefile);
				docInfo.setDocName(wordFileName[i]);
//				docInfo.setContentType(wordContentType[i]);
				
				System.err.println("wordContentType:"+wordContentType[i]);
				
				docInfo.setUploadRealName(realName);
				docInfo.setDocPath(realpath);
				docInfo.setUploadTime(new Date());
				docInfo.setDocumentType("word");
				docInfo.setSize(word[i].length()/1024);
				docInfo.setThesis(thesis);
				paperWordDocuments.add(docInfo);
			}
	   	System.out.println("上传验收文档中文档成功");
      	System.out.println(realpath);
		}
//		conferencePaper.setPaperType(PaperType.会议论文);
		thesisService.save(thesis);
		for(ThesisDocument doc:paperWordDocuments)
		{
			thesisDocService.save(doc);
		}
		for(ThesisDocument doc:paperPDFDocuments)
		{
			thesisDocService.save(doc);
		}
    	return "success";
	}
	/**
	 * 
	 *删除项目文档Ajax
	 * */
	public String deleteDocument() throws IOException
	{
		System.out.println("in del");
		System.out.println(document.getId());
		document = thesisDocService.get(ThesisDocument.class, document.getId());
		thesisDocService.delete(document);
		ActionContext ctx = ActionContext.getContext();     
	    HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
	    response.setContentType("text/html;charset=utf-8");
	    PrintWriter out=response.getWriter();
	    response.setContentType("text/html;charset=utf-8");
	    out.println("成功删除");
		return null;
	}
	/**
	 * 
	 *删除项目Ajax
	 * */
	public String delete() throws IOException
	{
		thesis = thesisService.get(Thesis.class,thesis.getId());
		thesisService.delete(thesis);
		ActionContext ctx = ActionContext.getContext();     
	    HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
	    response.setContentType("text/html;charset=utf-8");
	    PrintWriter out=response.getWriter();
	    response.setContentType("text/html;charset=utf-8");
	    out.println("成功删除");
		return null;
	}

	public static String getExt(String fileName)
	{
		return fileName.substring(fileName.lastIndexOf("."));
	}

	public IThesisService getThesisService() {
		return thesisService;
	}
	public void setThesisService(IThesisService thesisService) {
		this.thesisService = thesisService;
	}
	public IThesisDocService getThesisDocService() {
		return thesisDocService;
	}
	public void setThesisDocService(IThesisDocService thesisDocService) {
		this.thesisDocService = thesisDocService;
	}

	public Thesis getThesis() {
		return thesis;
	}
	public void setThesis(Thesis thesis) {
		this.thesis = thesis;
	}
	public File[] getWord() {
		return word;
	}
	public void setWord(File[] word) {
		this.word = word;
	}
	public String[] getWordContentType() {
		return wordContentType;
	}
	public void setWordContentType(String[] wordContentType) {
		this.wordContentType = wordContentType;
	}
	public String[] getWordFileName() {
		return wordFileName;
	}
	public void setWordFileName(String[] wordFileName) {
		this.wordFileName = wordFileName;
	}
	public File[] getPdf() {
		return pdf;
	}
	public void setPdf(File[] pdf) {
		this.pdf = pdf;
	}
	public String[] getPdfContentType() {
		return pdfContentType;
	}
	public void setPdfContentType(String[] pdfContentType) {
		this.pdfContentType = pdfContentType;
	}
	public String[] getPdfFileName() {
		return pdfFileName;
	}
	public void setPdfFileName(String[] pdfFileName) {
		this.pdfFileName = pdfFileName;
	}
	public ThesisDocument getDocument() {
		return document;
	}
	public void setDocument(ThesisDocument document) {
		this.document = document;
	}
}
